/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei2003.student@sina.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/

#include "sys.h"
#include "fork.h"
#include "record.h"
#include "mainwindow.h"
#include "header.h"
#include "config.h"

Record::Record (QObject * parent, const char *name):
QObject (parent, name),
mainwindow ((Sopcast *) parent)
{
  recordfp = new QFile ();
}

Record::~Record ()
{
  if (recordfp->exists ())
    {
      recordfp->close ();
      ::close (snrecord->socket ());
      delete snrecord;
    }
  delete recordfp;
}

void
Record::recordstreamready (int fd)
{
  int ret;
  char buff[1024];
  ret =::read (fd, buff, sizeof (buff));
  if (ret <= 0)
    {
      if (ret < 0)
	perror ("record socketnotifier");

      quitrecord ();
      /////restart record
      if (fork->snstatus)
	{
	  QTimer::singleShot (2000, this, SLOT (launchrecord ()));
	  mainwindow->buttonrecord->setPaletteForegroundColor (Qt::green);
	}

      return;
    }
  ::write (recordfp->handle (), buff, ret);
}


void
Record::launchrecord ()
{
  mainwindow->buttonrecord->setPaletteForegroundColor (Qt::black);

  int sockfd = Sys::connect_to_server ("127.0.0.1", atoi (fork->outport));
  if (sockfd < 0)
    {
      return;
    }
  ::write (sockfd, "1",::strlen ("1") + 1);
  char buf[19];
  if (::recv (sockfd, buf, sizeof (buf), 0) != sizeof (buf))
    {
      fprintf (stderr, "err while parseing head\n");
      ::close (sockfd);
      return;
    }
  int nread;
  ::ioctl (sockfd, FIONREAD, &nread);
  if (nread <= 0)
    {
      ::close (sockfd);

      if (nread < 0)
	perror ("ioctl get nread of socket");
      else if (fork->snstatus)
	{
	  QTimer::singleShot (2000, this, SLOT (launchrecord ()));
	  mainwindow->buttonrecord->setPaletteForegroundColor (Qt::green);
	}
      return;
    }
  ////data availible

  QString data_path = vboxconfig->editrecorddir->text ();
  QDir dir (data_path);
  if (!dir.exists ())
    dir.mkdir (data_path);
  data_path += mainwindow->channelname.replace (QRegExp ("\\/+"), "#");	///eliminate harmful character
  data_path += QDateTime::currentDateTime ().toString ();
  data_path += ".asf";

  recordfp->setName (data_path);
  if (!recordfp->open (IO_WriteOnly))
    {
      perror ("record path is invalid");
      ::close (sockfd);
      return;
    }

  snrecord = new QSocketNotifier (sockfd, QSocketNotifier::Read, this);
  connect (snrecord, SIGNAL (activated (int)),
	   SLOT (recordstreamready (int)));

  mainwindow->buttonrecord->setPaletteForegroundColor (Qt::red);
  return;
}

void
Record::quitrecord ()
{
  if (recordfp->exists ())
    {
      recordfp->close ();
      ::close (snrecord->socket ());
      delete snrecord;
      mainwindow->buttonrecord->setPaletteForegroundColor (Qt::black);
    }
}

void
Record::onbuttonrecordclicked ()
{
  if (mainwindow->buttonrecord->paletteForegroundColor () == Qt::black)
    {
      launchrecord ();
    }
  else if (mainwindow->buttonrecord->paletteForegroundColor () == Qt::red)
    {
      quitrecord ();
    }
  else if (mainwindow->buttonrecord->paletteForegroundColor () == Qt::green)
    {
      printf ("auto relaunch recording\n");
    }

}
